package com.alquiler.services;

import java.util.Date;
import java.util.List;

import com.alquiler.dao.ReservaDao;
import com.alquiler.entities.Reserva;
import com.alquiler.managers.SimpleEntityManager;
import com.alquiler.types.FORMA_PAGO;

public class ReservaService {
	
	private ReservaDao reservaDao;	
	
	public ReservaService() {		
		this.reservaDao = new ReservaDao(SimpleEntityManager.getInstance().getEntityManager());		
	}
	
	
	public List<Reserva> findAll(){
        return reservaDao.findAll();
	}
	
	public Reserva findById(Integer idReserva){
        return reservaDao.findById(idReserva);
        
        
	}
	public List<Reserva> getReservasByUsuario(Integer idUser){
		List<Reserva> reservas  = null;
		try{
			reservas = reservaDao.getReservasByUsuario(idUser);
		}catch (Exception e) {
			return null;
		}
        return reservas;
	}
	
	
	
	public List<Reserva> getTopDiezReservasByCancha(Integer idCancha){	
		return reservaDao.getTopDiezReservasByCancha(idCancha);	
	}
	
	
	public List<Reserva> getReservasByCancha(Integer idCancha){	
		return reservaDao.getReservasByCancha(idCancha);	
	}
	
	
	public List<Reserva> getReservasByfechaInicial(Date fechaIncial){		
		return reservaDao.getReservasByfechaInicial(fechaIncial);	
	}
	
	
	
	
	
	public void saveReserva(Reserva reserva){		
	
		
		try{
            SimpleEntityManager.getInstance().beginTransaction();	          
            reservaDao.save(reserva);
            SimpleEntityManager.getInstance().commit();
        }catch(Exception e){
            e.printStackTrace();
            SimpleEntityManager.getInstance().rollBack();
        }		
	}
	
	public void updateReserva(Reserva reserva){
		
		try{
			SimpleEntityManager.getInstance().beginTransaction();
			reservaDao.update(reserva);				
		    SimpleEntityManager.getInstance().commit();
        }catch(Exception e){
            e.printStackTrace();
            SimpleEntityManager.getInstance().rollBack();
        }
		
	}
	
	
	public void deleteReserva(Reserva reserva) throws Exception{
		try{
				
			SimpleEntityManager.getInstance().beginTransaction();
			reservaDao.remove(reserva);	
			SimpleEntityManager.getInstance().commit();
		  
        }catch(Exception e){        	
            SimpleEntityManager.getInstance().rollBack();       
        }
		
	}
	
	
	public static FORMA_PAGO getValFormaPago(Integer num){
				
		switch (num) {
		case 1:
			return FORMA_PAGO.Tarjeta_Debito;
			
		case 2:
			return FORMA_PAGO.Tarjeta_Credito;
		
		case 3:
			return FORMA_PAGO.Efectivo;
		
		default:
			return null;
			
		}
		
	}

}
